package reflect;

import java.lang.reflect.Field;

/**
 * 通过访问机制访问属性
 */
public class ReflectDemo8 {
    public static void main(String[] args) throws Exception{
        Class cls = Class.forName("reflect.Person");
        Object obj = cls.newInstance();

        Field field = cls.getDeclaredField("name");
        field.setAccessible(true);
        field.set(obj,"sjh");
        String name = (String) field.get(obj);
        System.out.println("姓名:"+name);
        field.setAccessible(false);

        Field field1 = cls.getDeclaredField("age");
        field1.setAccessible(true);
        field1.set(obj,1000);
        int age = (int) field1.get(obj);
        System.out.println("年龄:"+age);
        field1.setAccessible(false);

        System.out.println(obj);

    }
}
